package com.harmonyos.cases.view;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.harmonyos.cases.bean.SettingBean;
import com.harmonyos.cases.constants.Constants;
import com.harmonyos.cases.listener.SettingResultListener;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

// 测试版本设置参数dialog
public class SettingDialog extends DialogWrapper {

    private SettingResultListener settingResultListener;
    private final JBTextField configUrlInput = new JBTextField();
    private final JBTextField ownerInput = new JBTextField();
    private final JBTextField repoInput = new JBTextField();
    private final JBTextField refInput = new JBTextField();

    private SettingBean settingBean;

    public SettingDialog() {
        super(true);
        setTitle("设置测试参数");
        init();
    }

    public SettingDialog(SettingBean setting) {
        super(true);
        setTitle("设置测试参数");
        this.settingBean = setting;
        init();
    }

    private JPanel buildPanel(JBTextField textField, String hint, String result, String emptyText) {
        textField.setText(result);
        textField.setPreferredSize(new Dimension(300, 40));
        textField.setFont(textField.getFont().deriveFont(12f));
        textField.getEmptyText().setText(emptyText);
        JTextPane configUrlHint = new JTextPane();
        configUrlHint.setText(hint);
        JPanel configUrlPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        configUrlPanel.add(configUrlHint);
        configUrlPanel.add(textField);
        return configUrlPanel;
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel jPanel = new JPanel(new VerticalFlowLayout());

        jPanel.add(buildPanel(configUrlInput, "config路径", settingBean == null ? "" : settingBean.getConfigUrl(), "Gitee仓库中config.json文件的原始数据路径"));
        jPanel.add(buildPanel(ownerInput, "用户名", settingBean == null ? "" : settingBean.getOwner(), "Gitee仓库用户名，可从仓库http链接中获取"));
        jPanel.add(buildPanel(repoInput, "仓库名", settingBean == null ? "" : settingBean.getRepo(), "Gitee仓库名，可从仓库http链接中获取"));
        jPanel.add(buildPanel(refInput, "分支", settingBean == null ? "" : settingBean.getRef(), "Gitee仓库分支名，master可不填"));

        return jPanel;
    }

    // 点击确定时写入本地文件
    @Override
    protected void doOKAction() {
        super.doOKAction();
        // 文件不存在则创建
        String testSettingPath = Constants.TEST_SETTING;
        if (!Files.exists(Path.of(testSettingPath))) {
            try {
                Files.createFile(Path.of(testSettingPath));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            // 读取原文件数据，修改后重新写入
            String settings = Files.readString(Path.of(testSettingPath));
            SettingBean setting = new SettingBean();
            ObjectMapper mapper = new ObjectMapper();
            if (settings != null && !settings.isEmpty()) {
                setting = mapper.readValue(settings, SettingBean.class);
            }
            setting.setConfigUrl(configUrlInput.getText());
            setting.setOwner(ownerInput.getText());
            setting.setRef(refInput.getText());
            setting.setRepo(repoInput.getText());
            String writeString = mapper.writeValueAsString(setting);
            Files.writeString(Path.of(testSettingPath), writeString);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 将结果回调出去
        if (settingResultListener != null) {
            settingResultListener.settingResult(configUrlInput.getText(), ownerInput.getText(), repoInput.getText(), refInput.getText());
        }
    }

    public void setSettingResultListener(SettingResultListener settingResultListener) {
        this.settingResultListener = settingResultListener;
    }
}
